import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Tag } from '@/assets/type/commonType'

export const useTagsStore = defineStore('tags', () => {
  const tags = ref<Tag[]>([
    {
      path: '/home',
      name: 'home',
      label: '首页',
      icon: 'home'
    }
  ])

  const currentMenu = ref<Tag | null>()

  const selectMenu = function(val: Tag){
    if (val.name === 'home'){
      currentMenu.value = null
    } else {
      currentMenu.value = val
      const index = tags.value.findIndex(item => item.name === val.name)
      index === -1 ? tags.value.push(val) : ''
    }
  }

  const closeTag = function (tag: Tag){
    const index = tags.value.findIndex(item => item.name === tag.name)
    if (index >= 0){
      tags.value.splice(index, 1)
    }
  }

  return {
    tags,
    currentMenu,
    selectMenu,
    closeTag
  }
})